package CH02链表结构.LeetCode链表;

import java.util.ArrayDeque;
import java.util.ArrayList;

public class LC1019 {
    public int[] nextLargerNodes(ListNode head) {
        ArrayList<Integer> list = new ArrayList<>();
        ListNode p = head;
        while (p != null) {
            list.add(p.val);
            p = p.next;
        }
        int[] nums = new int[list.size()];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = list.get(i);
        }
        ArrayDeque<Integer> stack = new ArrayDeque<>();
        int[] answer = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            while (!stack.isEmpty() && nums[i] > nums[stack.peek()]) {
                answer[stack.peek()] = nums[i];
                stack.pop();
            }
            stack.push(i);
        }
        return answer;
    }
}